package DBLayer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class DBEvents
{
	public static final int NO_COMMIT = -2;
	public static final int FATAL = -1;
	public static final int OK = 0;
	
	private List<ActionListener> listeners = null;

	private static DBConnection database;


	protected DBEvents(ActionListener parent)
	{
		addActionListener(parent);
		database = DBConnection.getInstance();
		if (database == null)
			tellListeners(DBConnection.getException().toString(),DBEvents.FATAL);
		else tellListeners("Database ready",DBEvents.OK);
	}

	public synchronized void addActionListener(ActionListener actionListener)
	{// sen initialisering
		if (listeners == null)
		{
			listeners = new ArrayList<ActionListener>();
		}
		listeners.add(actionListener);
	}

	public synchronized void removeActionListener(ActionListener actionListener)
	{// short-curcuit evaluering
		if ((listeners != null) && (listeners.size() > 0))
			listeners.remove(actionListener);
	}

	protected void tellListeners(String whatHappened, int id)
	{
		if (listeners != null){
			ActionEvent actionEvent = new ActionEvent(this,id,whatHappened);
			for (ActionListener act:listeners)
			{
				act.actionPerformed(actionEvent);
			}
		} // if
	}
	
}
